package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.GetCashTypeResponse;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ASUS on 2017/11/13.
 */

public class FiltrateAdapter extends BaseAdapter {
    public ArrayList<GetCashTypeResponse> getCashTypeResponses;
    private Context mContext;
    private boolean chooseType;//true是单选，false是多选

    public FiltrateAdapter(ArrayList<GetCashTypeResponse> getCashTypeResponses, Context context, boolean chooseType){
        this.getCashTypeResponses = getCashTypeResponses;
        mContext = context;
        this.chooseType = chooseType;
    }

    @Override
    public int getCount() {
        return getCashTypeResponses.size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.cash_type_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.name.setText(getCashTypeResponses.get(i).getName());
        if(getCashTypeResponses.get(i).isChoose()){
            holder.name.setBackgroundResource(R.drawable.bg_corners_100_3);
            holder.name.setTextColor(mContext.getResources().getColor(R.color.bg_white));
        } else {
            holder.name.setBackgroundResource(R.drawable.bg_corners_100_12);
            holder.name.setTextColor(mContext.getResources().getColor(R.color.lottery_black));
        }
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(chooseType){
                    for(GetCashTypeResponse getCashTypeResponse: getCashTypeResponses){
                        getCashTypeResponse.setChoose(false);
                    }
                }
                if(getCashTypeResponses.get(i).isChoose()){
                    getCashTypeResponses.get(i).setChoose(false);
                } else {
                    getCashTypeResponses.get(i).setChoose(true);
                }
                notifyDataSetChanged();
            }
        });

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.name)
        TextView name;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}